package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.DetectionCycle;
import com.ruoyi.system.domain.Person;
import com.ruoyi.system.service.IDetectionCycleService;
import com.ruoyi.system.service.IPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 类别管理Controller
 *
 * @author ruoyi
 * @date 2022-02-21
 */
@RestController
@RequestMapping("/system/cycle")
public class DetectionCycleController extends BaseController {
    @Autowired
    private IDetectionCycleService detectionCycleService;
    @Autowired
    private IPersonService personService;


    /**
     * 查询类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:cycle:list')")
    @GetMapping("/list")
    public TableDataInfo list(DetectionCycle detectionCycle) {
        startPage();
        List<DetectionCycle> list = detectionCycleService.selectDetectionCycleList(detectionCycle);
        return getDataTable(list);
    }


    /**
     * 查询所有类别
     */
    @PreAuthorize("@ss.hasPermi('system:cycle:listAll')")
    @GetMapping("/listAll")
    public AjaxResult listAll(DetectionCycle detectionCycle) {
        return AjaxResult.success(detectionCycleService.selectDetectionCycleList(detectionCycle));
    }

    /**
     * 根据登录用户获取不同类别
     */
    @PreAuthorize("@ss.hasPermi('system:cycle:listUserDeptAll')")
    @GetMapping("/listUserDeptAll")
    public AjaxResult listUserDeptAll(DetectionCycle detectionCycle) {
        return AjaxResult.success(detectionCycleService.listUserDeptAll(detectionCycle));
    }

    /**
     * 导出类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:cycle:export')")
    @Log(title = "类别", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DetectionCycle detectionCycle) {
        List<DetectionCycle> list = detectionCycleService.selectDetectionCycleList(detectionCycle);
        ExcelUtil<DetectionCycle> util = new ExcelUtil<DetectionCycle>(DetectionCycle.class);
        return util.exportExcel(list, "类别数据");
    }


    /**
     * 获取类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cycle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(detectionCycleService.selectDetectionCycleById(id));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('system:cycle:edit')")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DetectionCycle detectionCycle) {
//        if (detectionCycle.getId() == null) {
//            if (UserConstants.NOT_UNIQUE.equals(detectionCycleService.checkCategoryNameUnique(detectionCycle.getCategoryName()))) {
//                return AjaxResult.error("修改类别'" + detectionCycle.getCategoryName() + "'失败，类别名称已存在");
//            }
//        }
        detectionCycle.setUpdateBy(getUsername());
        return toAjax(detectionCycleService.updateDetectionCycle(detectionCycle));
    }

    /**
     * 新增类别
     */
    @PreAuthorize("@ss.hasPermi('system:cycle:add')")
    @Log(title = "类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DetectionCycle detectionCycle) {
        if (UserConstants.NOT_UNIQUE.equals(detectionCycleService.checkCategoryNameUnique(detectionCycle.getCategoryName()))) {
            return AjaxResult.error("新增类别'" + detectionCycle.getCategoryName() + "'失败，类别名称已存在");
        }
        if (StringUtils.isNotNull(detectionCycle.getStartTime())) {
            long time = DateUtils.parseDate(detectionCycle.getStartTime()).getTime();
            if (time >= new Date().getTime()) {
                return AjaxResult.error("开始时间必须小于当前时间。");
            }
        } else {
            return AjaxResult.error("开始时间不允许为空");
        }
        detectionCycle.setCreateBy(getUsername());
        return toAjax(detectionCycleService.insertDetectionCycle(detectionCycle));
    }

    /**
     * 删除类别
     */
    @PreAuthorize("@ss.hasPermi('system:cycle:remove')")
    @Log(title = "删除类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            List<Person> Personlists = personService.selectPersonList(new Person(Long.valueOf(ids[i] + ""))); //类别id
            if (Personlists.size() > 0) {
                return error("该类别已被重点人群引用，不能删除。");
            }
        }
        return toAjax(detectionCycleService.deleteDetectionCycleByIds(ids));
    }


}
